using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.Automation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ContosoDashboard.ContosoDashboardService;

namespace ContosoDashboard.Views
{
    public partial class Home : Page
    {
        public Home()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            lstBoxCustomers.ItemsSource = ReactiveContosoService.Current.Customers;
        }

        private void BtnCustomerDetailsClick(object sender, RoutedEventArgs e)
        {
            var c = ((FrameworkElement)sender).DataContext as CustomerInfo;

            Debug.Assert(NavigationService != null, "NavigationService != null");
            Debug.Assert(c != null, "c != null");
            NavigationService.Navigate(new Uri("Customer?customerId=" + c.Id, UriKind.RelativeOrAbsolute));
        }

        private void BtnExportExcelClick(object sender, RoutedEventArgs e)
        {
            if (!AutomationFactory.IsAvailable)
                return;

            if (!Application.Current.HasElevatedPermissions)
                return;

            var customers = ReactiveContosoService.Current.Customers.ToList();

            string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string saveDirectory = string.Format("{0}\\ContosoDashboard", myDocuments); 

            if (!Directory.Exists(saveDirectory))
                Directory.CreateDirectory(saveDirectory);

            string path = string.Format("{0}\\{1}", saveDirectory, "customers.xlsx");

            if (File.Exists(path))
                File.Delete(path);

            dynamic excel = AutomationFactory.CreateObject("Excel.Application");
            dynamic excelWorkBook = excel.Workbooks.Add();
            dynamic sheet = excelWorkBook.ActiveSheet();

            int i = 1;

            foreach (var item in customers)
            {
                dynamic cell = sheet.Cells[i, 1];
                cell.Value = item.Name;

                cell = sheet.Cells[i, 2];
                cell.Value = item.City;
                cell.ColumnWidth = 100;

                cell = sheet.Cells[i, 3];
                cell.Value = item.Phone;
                cell.ColumnWidth = 100;

                i++;
            }

            excelWorkBook.SaveAs(path);
            excelWorkBook.Close();
            excel.Quit();
            NotificationService.Current.ShowNotification("Excel file saved");
        }

        private void BtnOpenInExcelClick(object sender, RoutedEventArgs e)
        {
            if (!AutomationFactory.IsAvailable)
                return;

            if (!Application.Current.HasElevatedPermissions)
                return;

            dynamic excel = AutomationFactory.CreateObject("Excel.Application");

            var customers = ReactiveContosoService.Current.Customers.ToList();

            dynamic workbook = excel.workbooks;
            workbook.Add();

            dynamic sheet = excel.ActiveSheet;
            int i = 1;

            foreach (var item in customers)
            {
                dynamic cell = sheet.Cells[i, 1];
                cell.Value = item.Name;

                cell = sheet.Cells[i, 2];
                cell.Value = item.City;
                cell.ColumnWidth = 100;

                cell = sheet.Cells[i, 3];
                cell.Value = item.Phone;
                cell.ColumnWidth = 100;

                i++;
            }

            excel.Visible = true;
        }
    }
}
